Should I sacrifice code succintness to ensure the narrowest variable scope? [duplicate]
Posted
by
David Scholefield
on Programmers
See other posts from Programmers
or by David Scholefield
Published on 2014-08-21T12:26:39Z
Indexed on
2014/08/21
16:27 UTC
Read the original article
Hit count: 260
This question already has an answer here:
In many languages (e.g. both Perl and Java - which are the two languages I work most with) it is possible to narrow the scope of local variables by declaring them within a block.
Although it adds extra code length (the opening and closing block braces), and possibly reduces readability, should I create blocks purely to narrow the scope of variables to the statements that use the variables and to uphold the principle of narrowest scope or does this sacrifice succinctness and readability just to unnecessarily uphold an agreed 'best practice' principle?
I usually declare local variables to functions/methods at the start of the function to aid readability, but I could not do this, and just create blocks throughout the function and declare the variables throughout the code - within those blocks - to narrow their scope.
© Programmers or respective owner